/**
 * 解析指令
 * 去listener中执行对应的方法
 */
class CommondParser(val listener: ICommondListener) {

    fun parse(commond:String){
        //用空格分隔指令 获取字符串数组
        val components = commond.split(" ")
        //获取指令
        val cmd = components.first()
        when(cmd){
            "ls" -> { listener.listFile(components.size == 2 && components[1] == "-l") }
            "pwd" -> {listener.showCurrentPath()}
            "mkdir"-> {
                if (components.size != 2) throw CommondParameterIllegalException()
                listener.createDirectory(components[1])
            }
            "cd" -> {
                if (components.size != 2) throw CommondParameterIllegalException()
                listener.changeDirectoryTo(components[1])
            }
            "rm" -> {
                if (components.size != 2) throw CommondParameterIllegalException()
                listener.delete(components[1])
            }
            "cp" -> {
                if (components.size != 3) throw CommondParameterIllegalException()
                listener.copy(components[1],components[2])
            }
            "mv" -> {
                if (components.size != 3) throw CommondParameterIllegalException()
                listener.move(components[1],components[2])
            }
            "rn" -> {
                if (components.size != 3) throw CommondParameterIllegalException()
                listener.rename(components[1],components[2])
            }
            "cat" -> {
                if (components.size != 2) throw CommondParameterIllegalException()
                listener.readData(components[1])
            }
            "vim" -> {
                if (components.size != 2) throw CommondParameterIllegalException()
                listener.createFile(components[1])
            }
            else ->{
                throw CommondNotFoundException()
            }
        }
    }
}